package cn.iocoder.yudao.module.service.controller.admin.assistant;

import cn.iocoder.yudao.framework.common.enums.AccountStatusEnum;
import cn.iocoder.yudao.module.service.convert.assistant.AssistantConverter;
import cn.iocoder.yudao.module.service.dal.dataobject.introconfig.ServiceIntroConfigDO;
import cn.iocoder.yudao.module.service.dal.dataobject.sceneconfig.SceneConfigDO;
import cn.iocoder.yudao.module.service.dal.dataobject.welcomeconfig.ServiceWelcomeConfigDO;
import cn.iocoder.yudao.module.service.service.introconfig.ServiceIntroConfigService;
import cn.iocoder.yudao.module.service.service.sceneconfig.SceneConfigService;
import cn.iocoder.yudao.module.service.service.welcomeconfig.ServiceWelcomeConfigService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.annotation.security.PermitAll;
import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.service.enums.ErrorCodeConstants.ASSISTANT_NOT_USED;
import static cn.iocoder.yudao.module.service.enums.ErrorCodeConstants.USER_HAS_NO_ASSISTANT;

import cn.iocoder.yudao.module.service.controller.admin.assistant.vo.*;
import cn.iocoder.yudao.module.service.dal.dataobject.assistant.ServiceAssistantDO;
import cn.iocoder.yudao.module.service.service.assistant.ServiceAssistantService;

@Tag(name = "管理后台 - 客服列表")
@RestController
@RequestMapping("/service/assistant")
@Validated
public class ServiceAssistantController {

    @Resource
    private ServiceAssistantService assistantService;

    @Resource
    private SceneConfigService sceneConfigService;

    @Resource
    private ServiceWelcomeConfigService serviceWelcomeConfigService;

    @Resource
    private ServiceIntroConfigService serviceIntroConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建客服列表")
    @PreAuthorize("@ss.hasPermission('service:assistant:create')")
    public CommonResult<Long> createAssistant(@Valid @RequestBody ServiceAssistantSaveReqVO createReqVO) {
        return success(assistantService.createAssistant(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新客服列表")
    @PreAuthorize("@ss.hasPermission('service:assistant:update')")
    public CommonResult<Boolean> updateAssistant(@Valid @RequestBody ServiceAssistantSaveReqVO updateReqVO) {
        assistantService.updateAssistant(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除客服列表")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('service:assistant:delete')")
    public CommonResult<Boolean> deleteAssistant(@RequestParam("id") Long id) {
        assistantService.deleteAssistant(id);
        return success(true);
    }

    @PostMapping("/verify")
    @Operation(summary = "验证客服账号")
    public CommonResult<ServiceAssistantDO> verifyAssistant() {
        ServiceAssistantDO assistant = assistantService.getAssistantByUserId(getLoginUserId());
        if (Objects.isNull(assistant)) {
            return error(USER_HAS_NO_ASSISTANT);
        }
        if (!Objects.equals(assistant.getStatus(), AccountStatusEnum.USED.getStatus())) {
            return error(ASSISTANT_NOT_USED);
        }
        return success(assistant);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客服列表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('service:assistant:query')")
    public CommonResult<ServiceAssistantRespVO> getAssistant(@RequestParam("id") Long id) {
        ServiceAssistantDO assistant = assistantService.getAssistant(id);
        return success(BeanUtils.toBean(assistant, ServiceAssistantRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得客服列表分页")
    @PreAuthorize("@ss.hasPermission('service:assistant:query')")
    public CommonResult<PageResult<ServiceAssistantRespVO>> getAssistantPage(@Valid ServiceAssistantPageReqVO pageReqVO) {
        PageResult<ServiceAssistantDO> pageResult = assistantService.getAssistantPage(pageReqVO);
        Map<Long, SceneConfigDO> sceneConfigMap = sceneConfigService.getSceneConfigMap();
        Map<Long, ServiceWelcomeConfigDO> welcomeConfigMap = serviceWelcomeConfigService.getWelcomeConfigMap();
        Map<Long, ServiceIntroConfigDO> introConfigMap = serviceIntroConfigService.getIntroConfigMap();
        return success(AssistantConverter.INSTANCE.convert(pageResult, sceneConfigMap, welcomeConfigMap, introConfigMap));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得客服列表")
    @PreAuthorize("@ss.hasPermission('service:assistant:query')")
    public CommonResult<List<ServiceAssistantRespVO>> getAssistantList() {
        ServiceAssistantPageReqVO pageReqVO = new ServiceAssistantPageReqVO();
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ServiceAssistantDO> pageResult = assistantService.getAssistantPage(pageReqVO).getList();
        return success(BeanUtils.toBean(pageResult, ServiceAssistantRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出客服列表 Excel")
    @PreAuthorize("@ss.hasPermission('service:assistant:export')")
    @OperateLog(type = EXPORT)
    public void exportAssistantExcel(@Valid ServiceAssistantPageReqVO pageReqVO,
                                     HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ServiceAssistantDO> list = assistantService.getAssistantPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "客服列表.xls", "数据", ServiceAssistantRespVO.class,
                BeanUtils.toBean(list, ServiceAssistantRespVO.class));
    }

    @GetMapping("/get-enable-assistant")
    @Operation(summary = "获得在线客服")
    @PermitAll
    @Parameter(name = "tenantId", description = "租户ID", required = true)
    @Parameter(name = "assistantId", description = "客服ID")
    public CommonResult<ServiceAssistantRespVO> getEnableAssistant(@RequestParam("tenantId") Long tenantId, @RequestParam(value = "assistantId", required = false) Long assistantId) {
        ServiceAssistantDO result = assistantService.getEnableAssistant(assistantId, tenantId);
        return success(BeanUtils.toBean(result, ServiceAssistantRespVO.class));
    }


//    @GetMapping("/get-assistant-by-id")
//    @Operation(summary = "获取指定客服")
//    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @Parameter(name = "tenantId", description = "租户编号", required = true, example = "1024")
//    @PermitAll
//    public CommonResult<ServiceAssistantRespVO> getAssistant(@RequestParam("id") Long id, @RequestParam("tenantId") Long tenantId) {
//        ServiceAssistantDO assistant = assistantService.getAssistant(id, tenantId);
//        return success(BeanUtils.toBean(assistant, ServiceAssistantRespVO.class));
//    }


}